"""
客户端主架构
"""
import socket, json
host, port, sock = "127.0.0.1", 1046, None
def output(*info):
    match info[0]:
        case "0x0":
            print("没有接口")
        case "0x1":
            print("连接超时")
        case "0x2":
            print("网络错误")
        case "0x3":
            print("连接断开")
        case "1x0":
            print("回答过慢")
        case "nx0":
            print("未知错误:",info[1:])
        case _ if "x" not in info:
            if len(info) == 1 and info[0][-1] == "：":
                return input(*info)
            else:
                return print(*info)
reflections = {"装弹": 0, "装": 0, "Z": 0, "zd": 0, "z": 0, "0": 0,
               "防御": 1, "防": 1, "F": 1, "fy": 1, "f": 1, "1": 1,
               "攻击": 2, "开": 2, "K": 2, "gj": 2, "k": 2, "2": 2,
               "嘲讽": 3, "嘲": 3, "C": 3, "cf": 3, "c": 3, "3": 3}
for a,b,c,d,e in socket.getaddrinfo(host,port,socket.AF_UNSPEC,socket.SOCK_STREAM):
    try:sock = socket.socket(a, b, c)
    except OSError:sock = None;continue
    try:sock.connect(e)
    except OSError:sock.close();sock = None;continue
    break
if sock is None:output("0x0");exit()
output("0：装弹，1：防御，2：攻击，3：嘲讽")
def input_operation():
    """
    input operation
    """
    global ammo
    operation, target = output("输入操作："), dict()
    while True:
        if operation in reflections.keys():
            operation = reflections[operation]
            if operation == 2:
                while True:
                    target_player=output(str(list(enumerate(players,start=1)))+"：")
                    if target_player not in [str(i) for i in range(1,len(players)+1)]:break
                    try:target_ammo = int(output("对应弹量："))
                    except ValueError:break
                    if 0 > target_ammo or target_ammo > ammo:break
                    target[players[int(target_player)-1]] = target_ammo
                    ammo -= target_ammo
            elif operation == 0:ammo += 1
            break
        operation = output("重新输入：")
    return operation, target
try:
    while True:
        name = output("输入名字：")
        if not name:sock.close();exit()
        sock.send(name.encode())
        statu = sock.recv(1024)
        if statu != b"\x01": break
    is_dead = 1
    while True:
        ammo = 0
        if statu == b"\x00":
            output("回车开局：")
            sock.send(b"\x00")
            statu = sock.recv(1024)
        players = json.loads(statu.decode())
        while True:
            if is_dead == 0:response = (-1,0)
            else:response = input_operation()
            sock.send(json.dumps(response).encode())
            data = json.loads(sock.recv(4096).decode())
            output("收到结果：", data)
            players = list(data.keys())
            is_dead = data[name][2]
            if is_dead == 2 or is_dead == 3:
                output("本局结束\n");break
        answer = output("是否继续：")
        if answer == "是" or answer == "yes":
            sock.send(b"\x01")
            statu = sock.recv(1024)
        else:
            sock.send(b"\x02")
            break
except TimeoutError:sock.close();output("0x1")
except OSError:output("1x0")
except json.decoder.JSONDecodeError:output("0x2")
except ConnectionResetError:output("0x3")
except Exception as error:output("nx0", error)